/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webClient;

/**
 *
 * @author User
 */

import appClient.ConnectClient;
import engine.Singleton;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class HttpClient extends Thread {

    public static void main(String[] args) throws Throwable {
        ServerSocket ss = new ServerSocket(7777);
        while (true) {
            Socket s = ss.accept();
            System.err.println("Client accepted");
            SocketProcessor pr = new SocketProcessor(s);
            new Thread(pr).start();
        }
    }
    
    public  HttpClient() 
    {   
    }
    
    
    public void run()
    {
        try {
            ServerSocket ss = new ServerSocket(7777);
            while (true) {
                Socket s = ss.accept();
                System.err.println("Client accepted");
                SocketProcessor pr = new SocketProcessor(s);
                new Thread(pr).start();
            }
        } catch (Throwable ex) {
            Logger.getLogger(HttpClient.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    public static class SocketProcessor implements Runnable {
 

        private Socket s;
        private InputStream is;
        private OutputStream os;
        private String data;
        public ConnectClient cli;
        public WebClientReaction wr;
        public WebPages wm;

        public SocketProcessor(Socket s) throws Throwable {
            this.s = s;
            this.is = s.getInputStream();
            this.os = s.getOutputStream();
            wm = new WebPages();
            data = wm.mainWindow();
            wr = new WebClientReaction(this);
        }
        

        

        public void run() {
            try {
                readInputHeaders();
               writeResponse(data);
            } catch (Throwable t) {
            } finally {
                try {
                    s.close();
                } catch (Throwable t) {
                }
            }
            System.err.println("Client processing finished");
            
        }

        public void writeResponse(String s) throws Throwable {
            String response = "HTTP/1.1 200 OK\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + s.length() + "\r\n" +
                    "Connection: close\r\n\r\n";

            String result = response + s;
            os.write(result.getBytes());
            os.flush();
        }

        private void readInputHeaders() throws Throwable {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            int i =0;
            String res = "";
            while(true) {
                String s = br.readLine();
                if (i==0) {
                    res = s;
                }
                if(s == null || s.trim().length() == 0) {
                    break;
                }
                i++;
            }
            System.out.println(res); /////////////////
            ////////////////// nado
            
            wr.getString(res, this);
        }

        public void setClient(ConnectClient cli) {
          this.cli = cli;
          cli.setSocProc(this);
        }

    
    }
}
